<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="title" label="标题" show-overflow-tooltip>
        <template slot-scope="{ row }">
          <span class="txt-hover" @click="handleDetail(row)">
            <el-tag
              class="tag"
              size="mini"
              effect="dark"
              type="danger"
              v-if="row.orderBy != 0"
              disable-transitions
              >置顶</el-tag
            >
            <el-tag
              class="tag"
              size="mini"
              effect="dark"
              type="warning"
              v-if="row.selection != 0"
              disable-transitions
              >精选</el-tag
            >
            {{ row.title }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="classifyId"
        label="分类"
        width="120"
        show-overflow-tooltip=""
      >
        <template slot-scope="{ row }">
          <span>{{ row.classifyId ? row.sysClassify.name : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="发布状态" width="100">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="审核状态" width="100">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus == 1" class="color-info">待审核</span>
          <span v-if="row.auditStatus == 2" class="color-success">已通过</span>
          <span v-if="row.auditStatus == 3" class="color-danger">未通过</span>
          <el-popover
            placement="top"
            title="审核说明"
            width="300"
            trigger="hover"
            style="margin-left: 5px"
            v-if="auditReason(row)"
            :content="auditReason(row)"
          >
            <i class="el-icon-info" slot="reference"></i>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handleDetail(row)"
              >详情</el-button
            >
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-popconfirm title="确定删除吗？" @confirm="handleDelete(row)">
              <el-button type="danger" slot="reference" size="mini">
                删除
              </el-button>
            </el-popconfirm>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import setInfo from '@/mixins/setInfo'
export default {
  mixins: [setInfo],
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        isAll: 'N',
      },
      tableData: [],
      pageObj: {},
    }
  },
  computed: {
    auditReason() {
      return (row) => {
        if (!row.extFiled) return
        let extFiled = JSON.parse(row.extFiled)
        if (!extFiled.hasOwnProperty('auditReason')) return
        return extFiled.auditReason
      }
    },
  },
  mounted() {
    this.getList()
    this.$bus.$on('refreshBbsTopicList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    this.$bus.$off('refreshBbsTopicList')
  },
  methods: {
    async getList() {
      let res = await this.$store.dispatch('queryBbsTopic', this.listQuery)
      this.tableData = res.data
      this.pageObj = res
    },
    handleDetail(row) {
      const URL = this.$router.resolve({
        name: 'bbs-detail',
        query: { id: row.id },
      })
      window.open(URL.href, '_blank')
    },
    handleEdit(row) {
      this.$bus.$emit('showDialogBbsAdd', row)
    },
    async handleDelete(row) {
      await this.$store.dispatch('delBbsTopic', row)
      this.getList()
    },
    //发布
    async handleStatus(row) {
      let { id, status } = row
      await this.$store.dispatch('updateBbsTopic', { id, status })
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss">
.tag {
  margin-right: 5px;
}
</style>